home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / news / inn1.000 / inn1.4sec-linux-src.tar / inn / include / macros.h < prev    next >
C/C++ Source or Header  |  1992-07-24  |  3KB  |  125 lines

  1. /*  $Revision: 1.13 $
  2. **
  3. **  Here be some useful macros.
  4. */
  5.  
  6.  
  7. /*
  8. **  Memory allocation.  Wrappers around wrapper functions.
  9. **  Don't replace any existing definitions, for use with malloc-debug
  10. **  packages, e.g.
  11. */
  12. #if    defined(_DEBUG_MALLOC_INC)
  13. #undef _DEBUG_MALLOC_INC
  14. #include "malloc.h"
  15. #else
  16. #define malloc_enter(func)
  17. #define malloc_leave(func)
  18. #define malloc_chain_check()
  19. #define malloc_dump(fd)
  20. #define malloc_list(a,b,c)
  21. #define malloc_size(hist)    (*(hist) = 0, 0)
  22. #endif    /* defined(_DEBUG_MALLOC_INC) */
  23.  
  24. #if    !defined(NEW)
  25.  
  26. #define NEW(T, c)            \
  27.     ((T *)xmalloc((unsigned int)(sizeof (T) * (c))))
  28.  
  29. #define RENEW(p, T, c)            \
  30.         (p = (T *)xrealloc((char *)(p), (unsigned int)(sizeof (T) * (c))))
  31.  
  32. /* =()<#define DISPOSE(p)        free((@<POINTER>@ *)p)>()= */
  33. #define DISPOSE(p)        free((char *)p)
  34.  
  35.     /* This properly belongs in libinn.h. */
  36. extern int (*xmemfailure)();
  37. #define ONALLLOCFAIL(func)        (xmemfailure = (func))
  38.  
  39. #endif    /* !defined(NEW) */
  40.  
  41.  
  42. /*
  43. **  Copy a string to allocated memory.
  44. */
  45. #define COPY(p)                \
  46.     strcpy(NEW(char, strlen(p) + 1), p)
  47.  
  48.  
  49. /*
  50. **  Wrappers around str[n]cmp.  Don't add the ((a) == (b)) test here; it's
  51. **  already been done in places where it's time-critical.
  52. */
  53. #define EQ(a, b)        (strcmp((a), (b)) == 0)
  54. #define EQn(a, b, n)        (strncmp((a), (b), (SIZE_T)(n)) == 0)
  55. #define caseEQ(a, b)        (strcasecmp((a), (b)) == 0)
  56. #define caseEQn(a, b, n)    (strncasecmp((a), (b), (SIZE_T)(n)) == 0)
  57.  
  58.  
  59. /*
  60. **  Cast a pointer into another point, but keep lint quiet.
  61. */
  62. #if    !defined(lint)
  63. #define CAST(t, p)    ((t)(p))
  64. #else
  65. #define CAST(t, p)    ((p) ? (t)NULL : (t)NULL)
  66. #endif /* !defined(lint) */
  67.  
  68.  
  69. /*
  70. **  <ctype.h> usually includes \n, which is not what we want.
  71. */
  72. #define ISWHITE(c)            ((c) == ' ' || (c) == '\t')
  73.  
  74.  
  75. /*
  76. **  Get the number of elements in a fixed-size array, or a pointer just
  77. **  past the end of it.
  78. */
  79. #define SIZEOF(array)    ((int)(sizeof array / sizeof array[0]))
  80. #define ENDOF(array)    (&array[SIZEOF(array)])
  81.  
  82.  
  83. /*
  84. **  Get the length of a string constant.
  85. */
  86. #define STRLEN(string)    ((int)(sizeof string - 1))
  87.  
  88.  
  89. /*
  90. **  Turn a TIMEINFO into a floating point number.
  91. */
  92. #define TIMEINFOasDOUBLE(t)    \
  93.     ((double)(t).time + ((double)(t).usec) / 1000000.0)
  94.  
  95.  
  96. /*
  97. **  Test data from a stat(2) call to see if it's a file or directory.
  98. */
  99. #if    !defined(S_ISDIR)
  100. #define S_ISDIR(st_mode)    (((st_mode) & S_IFMT) == S_IFDIR)
  101. #endif    /* !defined(S_ISDIR) */
  102. #if    !defined(S_ISREG)
  103. #define S_ISREG(st_mode)    (((st_mode) & S_IFMT) == S_IFREG)
  104. #endif    /* !defined(S_ISREG) */
  105.  
  106.  
  107. /*
  108. **  Get the size when binding an AF_UNIX socket.
  109. */
  110. #if    defined(DO_BIND_USE_SIZEOF)
  111. #define AF_UNIX_SOCKSIZE(S)    (sizeof S)  
  112. #else
  113. #define AF_UNIX_SOCKSIZE(S)    (sizeof S.sun_family + strlen(S.sun_path) + 1)
  114. #endif    /* defined(DO_BIND_USE_SIZEOF) */
  115.  
  116.  
  117. /*
  118. **  Use a read or recv call to read a descriptor.
  119. */
  120. #if    defined(DO_HAVE_UNIX_DOMAIN)
  121. #define RECVorREAD(fd, p, s)    recv((fd), (p), (s), 0)
  122. #else
  123. #define RECVorREAD(fd, p, s)    read((fd), (p), (s))
  124. #endif    /* defined(DO_HAVE_UNIX_DOMAIN) */
  125.